<template>
    <div>
         <div class="tableHeader">
            <div class="tableHeaderTitle">扩展模块列表</div>
            <div class="tableHeaderMenu">
                <el-button-group>
                    <el-button icon="el-icon-refresh" size="mini"></el-button>
                </el-button-group>
            </div>
        </div> 
        <el-table
                    ref="multipleTable"
                    :data="extensionModuleData"
                    style="width: 100%">
                <el-table-column
                        prop="fm_id"
                        label="模块ID"
                        width="80">
                </el-table-column>
                <el-table-column
                        prop="fm_name"
                        label="模块名称">
                </el-table-column>
                <el-table-column
                        prop="fm_dir_name"
                        label="项目名称">
                </el-table-column>
                <el-table-column label="操作" fixed="right" width="520">
                    <template slot-scope="scope">
                        <el-button
                                v-if="scope.row.fm_status == 0"
                                size="mini"
                                @click="handleInstall(scope.$index, scope.row)">安装扩展接口</el-button>
                        <el-button
                                v-if="scope.row.fm_status == 1"
                                size="mini"
                                type="danger"
                                @click="handleUninstall(scope.$index, scope.row)">卸载扩展接口</el-button>
                        <el-button
                                v-if="scope.row.fm_is_menu == 0"
                                size="mini"
                                @click="handleInstallMenu(scope.$index, scope.row)">安装菜单</el-button>   
                        <el-button
                                v-if="scope.row.fm_is_menu == 1"
                                size="mini"
                                type="danger"
                                @click="handleUninstallMenu(scope.$index, scope.row)">卸载菜单</el-button>
                        <el-button
                                v-if="scope.row.fm_is_manager == 1"
                                size="mini"
                                @click="handleDownloadCode(scope.$index, scope.row)">下载管理界面代码</el-button>         
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleRemove(scope.$index, scope.row)">删除模块管理</el-button>
                    </template>
                </el-table-column>
            </el-table>
    </div>
</template>

<script>
    import { mapState,mapMutations } from 'vuex'
    export default {
        //数据源
        data(){
            return {

            }
        },
        //初始化函数
        mounted() {

            //获取角色列表信息
            this.$myLoading.startLoading()
            this.$http.getHttp(this.$API.sysOrganizationalFunctionModuleSearch + "/skip/0/limit/1000", rs => {
           
                this.$myLoading.endLoading()
                this['ExtensionFunctionModuleListModule/udpateExtensionModuleData'](rs.data)

            });

        },
        //计算属性
        computed: {
            ...mapState({
                extensionModuleData : state => state.ExtensionFunctionModuleListModule.extensionModuleData
            })
        },
        //组件方法
        methods: {
            ...mapMutations(['udpateExtensionModuleData','ExtensionFunctionModuleListModule/udpateExtensionModuleData']),
        
            handleInstall(index,item){

                var params = {
                    fm_id          : item.fm_id,
                    fm_fmid        : item.fm_fmid,
                    fm_name        : item.fm_name,
                    fm_install_dir : item.fm_install_dir,
                    fm_dir_name    : item.fm_dir_name
                }

                this.$myLoading.startLoading()
                this.$http.postHttp(this.$API.systemApiInstallAggregationExtension,params, rs => {
                    this.$myLoading.endLoading()

                    var actionResult = rs.actionResult
                    var htmlStr = ""
                    for(var i in actionResult){
                    var colorStr = actionResult[i] == '[成功]' ? 'color:green' : 'color:red'
                    htmlStr += `
                        <p>${i}->结果：<span style='${colorStr}'>${actionResult[i]}</span></p>
                        `
                    }
                    this.$myPop.alertHtmlBox("模块安装结果",htmlStr)

                    //更新模块状态信息
                    item.fm_status = 1

                });

            },

            handleUninstall(index,item){

                var params = {
                    fm_id          : item.fm_id,
                    fm_dir_name    : item.fm_dir_name
                }

                this.$myLoading.startLoading()
                this.$http.postHttp(this.$API.systemApiUninstallAggregationExtension,params, rs => {
                    this.$myLoading.endLoading()
                    this.$myPop.SuccessMessage()

                    //更新模块状态信息
                    item.fm_status = 0

                });
                
            },

            //安装菜单
            handleInstallMenu(index,item){

                this.$myPop.confirmBox("安装提示","温馨提示：如果您不准备下载并使用管理端代码进行界面的整合，就不需要进行菜单安装，即使安装成功也没有办法进行使用，该操作为整合前端管理界面的前置操作",()=>{

                    var params = {
                        fm_id : item.fm_id,
                        fm_install_dir : item.fm_install_dir
                    }

                    //安装菜单操作
                    this.$myLoading.startLoading()
                    this.$http.postHttp(this.$API.systemApiInstallExtensionMenu,params, rs => {
                        this.$myLoading.endLoading()
                        this.$myPop.SuccessMessage()

                        //更新模块状态信息
                        item.fm_is_menu = 1

                    });

                });


            },

            //卸载菜单
            handleUninstallMenu(index,item){

                var params = {
                    fm_id : item.fm_id
                }

                //安装菜单操作
                this.$myLoading.startLoading()
                this.$http.postHttp(this.$API.systemApiUninstallExtensionMenu,params, rs => {
                    this.$myLoading.endLoading()
                    this.$myPop.SuccessMessage()

                    //更新模块状态信息
                    item.fm_is_menu = 0

                });

            },

            handleDownloadCode(){

            },

            handleRemove(index,item){

                //判断是否卸载了模块
                if(item.fm_status == 1){
                    this.$myPop.WarningMessage("请先卸载模块，在删除该模块信息")
                    return
                }

                if(item.fm_is_menu == 1){
                    this.$myPop.WarningMessage("请先卸载菜单信息，在删除该模块信息")
                    return
                }


                this.$myPop.confirmBox("删除提示","您确定要删除该扩展模块吗？",()=>{

                    var params = {
                        update_where : "fm_id",
                        fm_id : item.fm_id,
                        fm_is_delete : 1
                    }

                    this.$myLoading.startLoading()
                    this.$http.postHttp(this.$API.sysOrganizationalFunctionModuleUpdate,params, rs => {
                        this.$myLoading.endLoading()
                        this.$myPop.SuccessMessage()

                        this.extensionModuleData.splice(index,1)
                    })
                })
            }


        }
    }
</script>

<style lang="stylus" scoped>

</style>